package com.seari.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.MessageBean;
import com.seari.service.DkfyService;
import com.seari.util.IData;

@Controller
@RequestMapping(value = "/dkfy")
public class DkfyController extends BaseController {
	

	@Autowired
	private DkfyService dkfyService;
	
	
	/**
	 * @return
	 * @throws Exception
	 * 防疫整体说明
	 */
	@RequestMapping(value = "/fyOverallExplain")
	@ResponseBody
	public MessageBean<IData> fyOverallExplain() throws Exception {
		
		MessageBean<IData> msg = new MessageBean<IData>();
		IData pd = IData.getInstance(request);
		List<IData> datas = dkfyService.fyOverallExplain(pd);
		msg.setRows(datas);
		return msg;
	}
	
	/**
	 * @return
	 * @throws Exception
	 * 健康统计板块
	 */
	@RequestMapping(value = "/fyHealthStatistics")
	@ResponseBody
	public MessageBean<Object> fyHealthStatistics() throws Exception {
		
		MessageBean<Object> msg = new MessageBean<Object>();
		IData pd = IData.getInstance(request);
		List<IData> datas = dkfyService.fyHealthStatistics(pd);
		
		List<Object> ratioList = new ArrayList<Object>();
		for (int i = 0; i < datas.size(); i++) {
			
			Object ratio = datas.get(i).get("ratio");
			if(ratio!= null){
				ratioList.add(ratio);
			}else{
				ratioList.add(0);
			}
		}
		msg.setRows(ratioList);
		return msg;
	}
	/**
	 * @return
	 * @throws Exception
	 * 集团员工复工趋势
	 */
	@RequestMapping(value = "/fyWorkTrend")
	@ResponseBody
	public MessageBean<Object> fyWorkTrend() throws Exception {
		
		MessageBean<Object> msg = new MessageBean<Object>();
		IData pd = IData.getInstance(request);
		List<IData> datas = dkfyService.fyWorkTrend(pd);
		HashMap<Object, List<Object>> resuatMap = new HashMap<Object,List<Object>>();
		ArrayList<Object> companyList = new ArrayList<Object>();
		ArrayList<Object> daysList = new ArrayList<Object>();
		
		List<Object> onDutyList = new ArrayList<Object>();
		List<Object> homeList = new ArrayList<Object>();
		List<Object> rateList = new ArrayList<Object>();
		
		List<Object> dataList = new ArrayList<Object>();
		HashMap<Object, List<Object>> dataMap = new HashMap<Object,List<Object>>();
		
		for (int i = 0; i < datas.size(); i++) {
			Object company = datas.get(i).get("company");
			Object days = datas.get(i).get("days");
			Object onDuty = datas.get(i).get("onDuty");
			Object home = datas.get(i).get("home");
			Object rate = datas.get(i).get("rate");
			
			companyList.add(company);
			daysList.add(days);
			

			
			onDutyList.add(onDuty);
			homeList.add(home);
			rateList.add(rate);
			
			if(onDutyList.size() == 10 ){
				
				dataMap.put("onDuty", onDutyList);
				dataMap.put("home", homeList);
				dataMap.put("rate", rateList);
				
				dataList.add(dataMap);
				
				 onDutyList = new ArrayList<Object>();
				 homeList = new ArrayList<Object>();
				 rateList  = new ArrayList<Object>();
				 dataMap = new HashMap<Object,List<Object>>();
			}
			
		}

		List companyList2 = getSingle(companyList);
		List daysList2 = getSingle(daysList);
		resuatMap.put("company", companyList2);
		resuatMap.put("days", daysList2);
		resuatMap.put("data", dataList);
		
		msg.setBasicData(resuatMap);
		return msg;
	}
	

	/**
	 * @return
	 * @throws Exception
	 * 员工复工及现场办公人数分布图
	 */
	@RequestMapping(value = "/radar")
	@ResponseBody
	public Map<String,Object> fecthRadarChartData() throws Exception {

		IData pd = IData.getInstance(request);
		List<IData> datas = dkfyService.fecthRadarChartData(pd);
		Map<String,Object> res = new HashMap<>();


		List<Object> workPeopleList = new ArrayList<>();
		List<Object> workinsiteList = new ArrayList<>();
		for (int i = 0; i < datas.size(); i++) {
			workPeopleList.add(datas.get(i).get("workPeople"));
			workinsiteList.add(datas.get(i).get("workinsite"));
		}

		res.put("resWorkData",workPeopleList);
		res.put("localWorkData",workinsiteList);
		return res;
	}
	
	/**
	 * @return
	 * @throws Exception
	 * 员工考勤区域分布图
	 * 
	 */
	@RequestMapping(value = "/fyStaffClockingIn")
	@ResponseBody
	public MessageBean<IData> fyStaffClockingIn() throws Exception {
		
		MessageBean<IData> msg = new MessageBean<IData>();
		IData pd = IData.getInstance(request);
		List<IData> datas = dkfyService.fyStaffClockingIn(pd);
		msg.setData(datas);
		return msg;
	}
	
	
	/**
	 * @return
	 * @throws Exception
	 * 员工考勤区域分布图地图
	 * 
	 */
	@RequestMapping(value = "/fyMapDistribution")
	@ResponseBody
	public MessageBean<Object> fyMapDistribution() throws Exception {
		
		MessageBean<Object> msg = new MessageBean<Object>();
		IData pd = IData.getInstance(request);
		List<IData> datas = dkfyService.fyMapDistribution(pd);
		
		msg.setData(datas);
		return msg;
	}
	
	

	public static ArrayList getSingle(ArrayList list){
	    ArrayList newList = new ArrayList();     //创建新集合
	    Iterator it = list.iterator();        //根据传入的集合(旧集合)获取迭代器
	    while(it.hasNext()){          //遍历老集合
	       Object obj = it.next();       //记录每一个元素
	        if(!newList.contains(obj)){      //如果新集合中不包含旧集合中的元素
	        	newList.add(obj);       //将元素添加
	        }
	     }
	     return newList;
	}

}
